#include "telephoneBook.h"
#include<stdio.h>
#include<string.h>
#include"telephoneBook.h"


void InitInof(Tinof *inof) {
    inof->Name = (char *)malloc(NameSize * sizeof(char));
    if (inof->Name == NULL) {
        printf("Name申请空间失败\n");
        return;
    }
    inof->Telephone = (char *)malloc(TelephoneSize * sizeof(char));
    if (inof->Telephone == NULL) {
        free(inof->Name);
        printf("Telephone申请空间失败\n");
        return;
    }
}

void InitTBook(TBook* book) {
    book->inof = (Tinof *)malloc(BookSize * sizeof(Tinof));
    if (book->inof == NULL) {
        printf("申请空间失败\n");
        return;
    }
    for (int i = 0; i < BookSize; i++) {
        InitInof(&(book->inof[i])); // 初始化数组中的每一个元素
    }
    book->size = 0;
    book->capacity = BookSize;
}
//显示所有信息
void ShowInof(Tinof *inof)
{
    printf("姓名：%s 电话号码：%s\n",inof->Name, inof->Telephone);
}
void Show(TBook *book)
{
    for (int i = 0; i < book->size;i++)
    {
        ShowInof((book->inof) + i);
    }
}

void SetinofValue(Tinof *inof, char* name, char* telephone)
{
    strcpy(inof->Name, name);

    strcpy(inof->Telephone, telephone);
}

// 插入新的联系人
void InsertInof(TBook *book, char* name, char *telephone)
{
    for (int i = 0; i < book->size;i++)
    {
        if(strcmp(book->inof->Name,name)==0&&strcmp(book->inof->Telephone,telephone)==0)
           {
             printf("您输入的联系人已存在！\n");
             return;
           }
    }
    if (book->size == book->capacity) {
        // 重新分配更大的空间
        Tinof *new_inof = realloc(book->inof, book->capacity * GrowthFactor * sizeof(Tinof));
        if (new_inof == NULL) {
            printf( "申请空间失败\n");
            return ;
        }
        book->inof = new_inof;
        book->capacity *= GrowthFactor;
    }
    SetinofValue(&book->inof[book->size], name, telephone);
    book->size++;
}

void RemoveByIndex(TBook *book, int index)
{
    for (int i = index; i < book->size - 1;i++)
    {
        book->inof[i] = book->inof[i + 1];
    }
    book->size--;
}
//按名字删除联系人
//按电话删除联系人
void RemovePeople(TBook *book, void* value, bool (*p)(Tinof *, void*))
{
    int flag = 0;
    for (int i = 0; i < book->size;i++)
    {
        if(p(&book->inof[i],value)==true)
        {
            RemoveByIndex(book, i);
            i--;
            flag = 1;
        }
    }
    if(flag==0)
    printf("您所输入的信息不存在，请重新输入。\n");
    
}
//支持模糊查询
//按电话查找联系人
//按名字查找联系人
void FindPeople(TBook *book, void* value, bool (*p)(Tinof *, void*))
{
    int flag = 0;
    for (int i = 0; i < book->size; i++)
    {
        if(p(&book->inof[i],value)==true)
        {
            ShowInof(&book->inof[i]);
            flag = 1;
        }
    }
    if(flag==0)
        printf("您所输入的信息不存在，请重新输入。\n");
}
//修改联系人名字和电话
void RevisePeople(TBook *book,void *value_revisewhat, void* value_old, void*value_new,bool (*p)(Tinof*, void*))
{
    bool found = false;
    
    if (strcmp(value_revisewhat, "姓名") == 0)
    {
        for (int i = 0; i < book->size;i++)
    {
        if (p(&book->inof[i], value_old) == true)
        {
            strcpy(book->inof[i].Name, (char*)value_new);
            found = true;
        }
    }
    }
        if(strcmp(value_revisewhat,"电话号码")==0)
    {
        for (int i = 0; i < book->size;i++)
    {
        if (p(&book->inof[i], value_old) == true)
        {
            strcpy(book->inof[i].Telephone, (char*)value_new);
            found = true;
        }
    }
    }

    if(found==false)
    {
    printf("您输入的%s不存在！\n",(char*)value_revisewhat);
    }
}
void FreeInof(Tinof *inof) {
    if (inof != NULL) {
        free(inof->Name);
        free(inof->Telephone);
    }
}
void FreeBook(TBook *book) {
    if (book != NULL && book->inof != NULL) {
        for (int i = 0; i < book->size; i++) {
            FreeInof(&book->inof[i]);
        }
        free(book->inof);
    }
}
